package com.smartum.mpr.business;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smartum.mpr.dao.CountryDao;
import com.smartum.mpr.dao.ItemCategoryDao;
import com.smartum.mpr.dao.JobCategoryDao;
import com.smartum.mpr.dao.JobSubCategoryDao;
import com.smartum.mpr.dao.StateDao;
import com.smartum.mpr.dao.ItemSubCategoryDao;
import com.smartum.mpr.dao.CountyDao;
import com.smartum.mpr.exception.NotExistsCategoryException;
import com.smartum.mpr.exception.NotExistsSubCategoryException;
import com.smartum.mpr.model.Country;
import com.smartum.mpr.model.ItemCategory;
import com.smartum.mpr.model.ItemSubCategory;
import com.smartum.mpr.model.JobCategory;
import com.smartum.mpr.model.JobSubCategory;
import com.smartum.mpr.model.State;
import com.smartum.mpr.model.County;

@Service
public class StaticDataBusiness extends BaseBusiness {

	@Autowired
	private ItemCategoryDao itemCategoryDao;
	@Autowired
	private ItemSubCategoryDao itemSubCategoryDao;

	@Autowired
	private JobCategoryDao jobCategoryDao;
	@Autowired
	private JobSubCategoryDao jobSubCategoryDao;

	@Autowired
	private CountyDao countyDao;
	@Autowired
	private StateDao stateDao;

	@Autowired
	private CountryDao countryDao;

	public List<ItemCategory> getItemCategories() {
		return itemCategoryDao.getList();

	}

	public List<ItemSubCategory> getItemSubCategories(long categoryID) {
		if (categoryID == 0) {
			return itemSubCategoryDao.getList();
		} else {
			return itemSubCategoryDao.getSubCategoriesByCategoryId(categoryID);
		}
	}

	public void addItemCategory(String name) {
		ItemCategory itemCategory = new ItemCategory();
		itemCategory.setCategoryName(name);
		itemCategoryDao.save(itemCategory);
	}

	public void addItemSubCategory(long categoryId, String name) throws NotExistsCategoryException {
		ItemCategory itemCategory = itemCategoryDao.get(categoryId);
		if (itemCategory == null) {
			throw new NotExistsCategoryException();
		}
		ItemSubCategory subCategory = new ItemSubCategory();
		subCategory.setCategory(itemCategory);
		subCategory.setSubCategoryName(name);
		itemSubCategoryDao.save(subCategory);
	}

	public void updateItemCategory(Long id, String name) throws NotExistsCategoryException {
		ItemCategory itemCategory = itemCategoryDao.get(id);
		if (itemCategory == null) {
			throw new NotExistsCategoryException();
		}
		itemCategory.setCategoryName(name);
		itemCategoryDao.update(itemCategory);
	}

	public void updateItemSubCategory(long subcategoryId, String name) throws NotExistsSubCategoryException {
		ItemSubCategory subCategory = itemSubCategoryDao.get(subcategoryId);
		if(subCategory==null){
			throw new NotExistsSubCategoryException();
		}
		subCategory.setSubCategoryName(name);
		itemSubCategoryDao.update(subCategory);
	}

	public void deleteItemCategory(Long id) throws NotExistsCategoryException {
		ItemCategory itemCategory = itemCategoryDao.get(id);
		if(itemCategory==null){
			throw new NotExistsCategoryException();
		}
		itemCategoryDao.delete(id);
	}

	public void deleteItemSubCategory(long subcategoryId) throws NotExistsSubCategoryException {
		ItemSubCategory subCategory = itemSubCategoryDao.get(subcategoryId);
		if (subCategory == null) {
			throw new NotExistsSubCategoryException();
		}
		itemSubCategoryDao.delete(subcategoryId);
	}
	
	
	public void addJobCategory(String name) {
		JobCategory jobCategory = new JobCategory();
		jobCategory.setCategoryName(name);
		jobCategoryDao.save(jobCategory);
	}

	public void addJobSubCategory(long categoryId, String name) throws NotExistsCategoryException {
		JobCategory jobCategory = jobCategoryDao.get(categoryId);
		if (jobCategory == null) {
			throw new NotExistsCategoryException();
		}
		JobSubCategory subCategory = new JobSubCategory();
		subCategory.setCategory(jobCategory);
		subCategory.setSubCategoryName(name);
		jobSubCategoryDao.save(subCategory);
	}

	public void updateJobCategory(Long id, String name) throws NotExistsCategoryException {
		JobCategory jobCategory = jobCategoryDao.get(id);
		if (jobCategory == null) {
			throw new NotExistsCategoryException();
		}
		jobCategory.setCategoryName(name);
		jobCategoryDao.update(jobCategory);
	}

	public void updateJobSubCategory(long subcategoryId, String name) throws NotExistsSubCategoryException {
		JobSubCategory subCategory = jobSubCategoryDao.get(subcategoryId);
		if(subCategory==null){
			throw new NotExistsSubCategoryException();
		}
		subCategory.setSubCategoryName(name);
		jobSubCategoryDao.update(subCategory);
	}

	public void deleteJobCategory(Long id) throws NotExistsCategoryException {
		JobCategory jobCategory = jobCategoryDao.get(id);
		if(jobCategory==null){
			throw new NotExistsCategoryException();
		}
		jobCategoryDao.delete(id);
	}

	public void deleteJobSubCategory(long subcategoryId) throws NotExistsSubCategoryException {
		JobSubCategory subCategory = jobSubCategoryDao.get(subcategoryId);
		if (subCategory == null) {
			throw new NotExistsSubCategoryException();
		}
		jobSubCategoryDao.delete(subcategoryId);
	}

	public ItemSubCategory getItemSubCategoryById(Long id) {
		return itemSubCategoryDao.get(id);
	}

	public List<JobCategory> getJobCategories() {
		return jobCategoryDao.getList();
	}

	public List<JobSubCategory> getJobSubCategories(long categoryID) {
		if (categoryID == 0) {
			return jobSubCategoryDao.getList();
		} else {
			return jobSubCategoryDao.getSubCategoriesByCategoryId(categoryID);
		}
	}

	public JobSubCategory getJobSubCategoryById(Long id) {
		return jobSubCategoryDao.get(id);
	}

	public List<State> getStates() {
		return stateDao.getList();

	}

	public List<County> getCounties(long stateID) {
		if (stateID == 0) {
			return countyDao.getList();
		} else {
			return countyDao.getCountiesByStateId(stateID);
		}
	}

	public List<Country> getCountries() {
		return countryDao.getList();
	}

	public Country getCountryById(Long id) {
		return countryDao.get(id);
	}

	public County getCountyById(Long id) {
		return countyDao.get(id);
	}

}
